<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   投注记录
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('lottery_order');
$oltable = Table('lottery_order_list');
$ltable = Table('lottery_period');
$ptable = Table('lottery_play');

if(IS_POST)
{
	//	是否暂停投注
	$tmp = $db->getrow('SELECT * FROM '.Table('user_status').' WHERE uid = '.$_uid);
	!$tmp['invest'] && AjaxReturn(1,'你的账户被暂停使用投注功能');
	
	if($a == 'add')
	{
		$cid = intval($_POST['gameid']);
		!$cid && AjaxReturn(1,'请选择彩票种类');
		
		$iid = intval($_POST['periodid']);
		!$iid && AjaxReturn(1,'请选择彩票期号');
		
		$nums = $_POST['orderlist'];
		$nums = json_decode($nums,true);
		!is_array($nums) && AjaxReturn(1,'请选择要投注的号码');
		
		$game = $lottery[$cid];
		!$game && AjaxReturn(1,'请选择彩票种类');
		
		//	期号
		$tmp = $db->getrow('SELECT id,period,stoptime FROM '.$ltable.' WHERE cid = '.$cid.' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime');
		!$tmp && AjaxReturn(1,'当前开盘期与投注期数不一致');
		($tmp['id'] != $iid) && AjaxReturn(1,'当前开盘期与投注期数不一致');
		$period = $tmp['period'];
		$stoptime = $tmp['stoptime'];
		
		//	下注
		//include_once(LIB_PATH.'lottery/'.$game['ename'].'_buy.inc.php');
		include_once(LIB_PATH.'lottery/cqssc_buy.inc.php');
		!$orders && AjaxReturn(1,'请选择要投注的号码');
		($money < 0) && AjaxReturn(1,'请选择要投注的号码');
		
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['tid']		=	'order_add';
		$tmp['lkey']	=	'order_add_'.$_uid.'_'.$cid.date('_YmdHis');
		$tmp['money']	=	$money;
		$tmp['balance']	=	-$money;
		$tmp['notes']	=	'下注购买彩票：'.$game['title'];
		$res = MoneyLog($tmp);
		!$res && AjaxReturn(1,'账户余额不足，请先充值');
		
		//	订单
		$orderno = date('YmdHis').str_pad($cid,2,0,STR_PAD_LEFT).str_pad($_uid,6,0,STR_PAD_LEFT).rand(1000,9999);
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['cid']		=	$cid;
		$tmp['iid']		=	$iid;
		$tmp['period']	=	$period;
		$tmp['orderno']	=	$orderno;
		$tmp['content']	=	json_encode($nums);
		$tmp['money']	=	$money;
		$tmp['dateline']=	TIMESTAMP;
		$orderid = $db->add('lottery_order',$tmp);
		//!$orderid && AjaxReturn(1,'程序大哥在打瞌睡，请重新提交');
		
		//	订单详单
		$res = [];	//	返回值
		foreach($orders as $key => $val)
		{
			$val['oid'] = $orderid;
			$oid = $db->add('lottery_order_list',$val);
			if(!$oid)
			{
				continue;
			}
			
			$tmp = [];
			$tmp['OrderId']			=	$oid;
			$tmp['Ordernumber']		=	$oid;
			$tmp['PeriodId']		=	$iid;
			$tmp['NumberOfPeriod']	=	$period;
			$tmp['GamePlayName']	=	$plays[$key]['name'];
			$tmp['CreateTime']		=	date('Y-m-d H:i:s');
			$tmp['Content']			=	$val['rednum'];
			$tmp['Times']			=	$val['times'];
			$tmp['MoneyModel']		=	$val['unit'];
			$tmp['BetNumber']		=	$val['multiple'];
			$tmp['Amount']			=	$val['money'];
			$tmp['KickBack']		=	0.0;
			$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
			$tmp['KickbackAmount']	=	0.0;
			$tmp['OpenResult']		=	'';//$val['multiple'];
			$tmp['BettingBalance']	=	bcadd($val['prize'],-$val['money']);
			$tmp['PeriodStatus']	=	1;
			$tmp['OrderStatus']		=	0;
			$tmp['IsTrack']			=	false;
			$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$stoptime);
			
			$res[] = $tmp;
		}
		
		//	{"status":true,"info":"投注成功！","insertedOrder":"[{\"OrderId\":148528595,\"Ordernumber\":\"73008251039323605066\",\"PeriodId\":107620684,\"NumberOfPeriod\":\"20170825028\",\"GamePlayName\":\"五星复式\",\"CreateTime\":\"2017-08-25 10:39:32\",\"Content\":\"5|7|2|5|0\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2.0,\"KickBack\":0.0,\"KickbackPercent\":0.0,\"KickbackAmount\":0.0,\"OpenResult\":\"\",\"BettingBalance\":0.0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25T10:39:40\"}]"}
		//	{"status":true,"info":"投注成功！","data":"[{\"OrderId\":\"42\",\"Ordernumber\":\"42\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900960\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"1|9,2,6\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"},{\"OrderId\":\"43\",\"Ordernumber\":\"43\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900930\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"8,0|4\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"}]"}
		AjaxReturn(0,'下注成功！',$res);
	}
	else if($a == 'track')	//	追号
	{
		$cid = intval($_POST['gameid']);
		!$cid && $cid = intval($_POST['gameId']);
		!$cid && AjaxReturn(1,'请选择彩票种类');
		
		$nums = $_POST['orderlist'];
		$nums = json_decode($nums,true);
		!is_array($nums) && AjaxReturn(1,'请选择要投注的号码');
		
		$game = $lottery[$cid];
		!$game && AjaxReturn(1,'请选择彩票种类');
		
		//	追号只要一组数据
		$nums = $nums[0];
		
		//	玩法、号码、注数
		$playid = intval($nums['i']);
		$rednum = NoBadStr($nums['c']);
		$multiple = max(1,intval($nums['n']));
		!$rednum && AjaxReturn(1,'请选择要投注的号码');
		
		$plays = $db->getrow('SELECT * FROM '.$ptable.' WHERE playid = '.$playid.' AND status = 1 AND cid = '.$cid);
		!$plays && AjaxReturn(1,'该玩法已经下架了');
		
		//	期号和倍数
		$tmp = $_POST['periodlist'];
		$tmp = json_decode($tmp,true);
		!is_array($tmp) && AjaxReturn(1,'请选择要追号的期数');
	
		//	单位1、2、3对应元角分
		$unit = intval($val['m']);
		$units = [1=>1,2=>0.1,3=>0.01];
		!array_key_exists($unit,$units) && $unit = 1;
		
		$qihaos = [];
		$money = 0;	//	总金额
		foreach($tmp as $val)
		{
			$iid = intval($val['PeriodId']);
			$times = max(1,intval($val['Times']));
			
			$t2 = $db->getrow('SELECT period,stoptime,status FROM '.$ltable.' WHERE id = '.$iid.' AND cid = '.$cid.' AND stoptime >= '.TIMESTAMP);
			if(!$t2)
			{
				continue;
			}
			
			$money2 = $multiple * $times * 2 * $units[$unit];
			
			$qihaos[] = ['iid'=>$iid,'period'=>$t2['period'],'times'=>$times,'money'=>$money2,'stoptime'=>$t2['stoptime'],'status'=>$t2['status']];			
			$money += $money2;
		}
		!$qihaos && AjaxReturn(1,'请选择要追号的期数');
		($money < 0) && AjaxReturn(1,'请选择要投注的号码');
		
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['tid']		=	'order_add';
		$tmp['lkey']	=	'order_add_'.$_uid.'_'.$cid.date('_YmdHis');
		$tmp['money']	=	$money;
		$tmp['balance']	=	-$money;
		$tmp['notes']	=	'下注购买彩票：'.$game['title'];
		$res = MoneyLog($tmp);
		!$res && AjaxReturn(1,'账户余额不足，请先充值');
		
		//	追单订单
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['cid']		=	$cid;
		$tmp['pid']		=	$playid;
		$tmp['period']	=	$qihaos[0]['period'];
		$tmp['rednum']	=	$rednum;
		$tmp['content']	=	json_encode(['content'=>$nums,'period'=>$qihaos]);
		$tmp['money']	=	$money;
		$tmp['okmoney']	=	0;
		$tmp['dateline']=	TIMESTAMP;
		$tmp['num']		=	count($qihaos);
		$tmp['oknum']	=	0;
		$tmp['stopskip']=	$_POST['stopAfterSkip'] == 'true' ? 1 : 0;
		$tmp['stopwin']	=	$_POST['stopAfterWin'] == 'true' ? 1 : 0;
		$orderid = $db->add('lottery_order_track',$tmp);
		!$orderid && AjaxReturn(1,'程序大哥在打瞌睡，请重新提交');
		
		//	订单详单
		$res = [];	//	返回值
		foreach($qihaos as $key => $val)
		{
			$tmp = [];
			$tmp['uid']		=	$_uid;
			$tmp['cid']		=	$cid;
			$tmp['iid']		=	$val['iid'];
			$tmp['pid']		=	$playid;
			$tmp['tid'] 	=	$orderid;
			$tmp['period']	=	$val['period'];
			$tmp['rednum']	=	$rednum;
			$tmp['times']	=	$val['times'];
			$tmp['multiple']=	$multiple;
			$tmp['unit']	=	$unit;
			$tmp['money']	=	$multiple * $val['times'] * 2 * $units[$unit];
			$tmp['dateline']=	TIMESTAMP;
			$oid = $db->add('lottery_order_list',$tmp);
			if(!$oid)
			{
				continue;
			}
			
			$tmp = [];
			$tmp['OrderId']			=	$oid;
			$tmp['Ordernumber']		=	$oid;
			$tmp['PeriodId']		=	$val['iid'];
			$tmp['NumberOfPeriod']	=	$val['period'];
			$tmp['GamePlayName']	=	$plays['name'];
			$tmp['CreateTime']		=	date('Y-m-d H:i:s');
			$tmp['Content']			=	$rednum;
			$tmp['Times']			=	$val['times'];
			$tmp['MoneyModel']		=	$unit;
			$tmp['BetNumber']		=	$multiple;
			$tmp['Amount']			=	$val['money'];
			$tmp['KickBack']		=	0.0;
			$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
			$tmp['KickbackAmount']	=	0.0;
			$tmp['OpenResult']		=	'';//$val['multiple'];
			$tmp['BettingBalance']	=	-$val['money'];
			$tmp['PeriodStatus']	=	$val['status'];
			$tmp['OrderStatus']		=	0;
			$tmp['IsTrack']			=	true;
			$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$val['stoptime']);
			
			$res[] = $tmp;
		}
		
		//	{"status":true,"info":"投注成功！","insertedOrder":"[{\"OrderId\":148528595,\"Ordernumber\":\"73008251039323605066\",\"PeriodId\":107620684,\"NumberOfPeriod\":\"20170825028\",\"GamePlayName\":\"五星复式\",\"CreateTime\":\"2017-08-25 10:39:32\",\"Content\":\"5|7|2|5|0\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2.0,\"KickBack\":0.0,\"KickbackPercent\":0.0,\"KickbackAmount\":0.0,\"OpenResult\":\"\",\"BettingBalance\":0.0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25T10:39:40\"}]"}
		//	{"status":true,"info":"投注成功！","data":"[{\"OrderId\":\"42\",\"Ordernumber\":\"42\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900960\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"1|9,2,6\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"},{\"OrderId\":\"43\",\"Ordernumber\":\"43\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900930\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"8,0|4\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"}]"}
		AjaxReturn(0,'下注成功！',$res);
	}
	else if($a == 'xgssc')	//	香港时时彩
	{
		$cid = 23;
		
		$iid = intval($_POST['periodID']);
		!$iid && AjaxReturn(1,'请选择彩票期号');
		
		$nums = $_POST['orderlist'];
		$nums = json_decode($nums,true);
		!is_array($nums) && AjaxReturn(1,'请选择要投注的号码');
		
		$game = $lottery[$cid];
		!$game && AjaxReturn(1,'请选择彩票种类');
		
		//	期号
		$tmp = $db->getrow('SELECT id,period,stoptime FROM '.$ltable.' WHERE cid = '.$cid.' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime');
		!$tmp && AjaxReturn(1,'当前开盘期与投注期数不一致');
		($tmp['id'] != $iid) && AjaxReturn(1,'当前开盘期与投注期数不一致');
		$period = $tmp['period'];
		$stoptime = $tmp['stoptime'];
		
		//	是否最新赔率
		$rateforce = $_POST['force'] == 'true' ? 1 : 0;
		
		//	下注
		//include_once(LIB_PATH.'lottery/'.$game['ename'].'_buy.inc.php');
		include_once(LIB_PATH.'lottery/xgssc_buy.inc.php');
		!$orders && AjaxReturn(1,'请选择要投注的号码');
		($money < 0) && AjaxReturn(1,'请选择要投注的号码');
		
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['tid']		=	'order_add';
		$tmp['lkey']	=	'order_add_'.$_uid.'_'.$cid.date('_YmdHis');
		$tmp['money']	=	$money;
		$tmp['balance']	=	-$money;
		$tmp['notes']	=	'下注购买彩票：'.$game['title'];
		$res = MoneyLog($tmp);
		!$res && AjaxReturn(1,'账户余额不足，请先充值');
		
		//	订单
		$orderno = date('YmdHis').str_pad($cid,2,0,STR_PAD_LEFT).str_pad($_uid,6,0,STR_PAD_LEFT).rand(1000,9999);
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['cid']		=	$cid;
		$tmp['iid']		=	$iid;
		$tmp['period']	=	$period;
		$tmp['orderno']	=	$orderno;
		$tmp['content']	=	json_encode($nums);
		$tmp['money']	=	$money;
		$tmp['dateline']=	TIMESTAMP;
		$orderid = $db->add('lottery_order',$tmp);
		//!$orderid && AjaxReturn(1,'程序大哥在打瞌睡，请重新提交');
		
		//	订单详单
		$res = [];	//	返回值
		foreach($orders as $key => $val)
		{
			$val['oid'] = $orderid;
			$oid = $db->add('lottery_order_list',$val);
			if(!$oid)
			{
				continue;
			}
			
			$tmp = [];
			$tmp['OrderId']			=	$oid;
			$tmp['Ordernumber']		=	$oid;
			$tmp['PeriodId']		=	$iid;
			$tmp['NumberOfPeriod']	=	$period;
			$tmp['GamePlayName']	=	$plays[$key]['name'];
			$tmp['CreateTime']		=	date('Y-m-d H:i:s');
			$tmp['Content']			=	$val['rednum'];
			$tmp['Times']			=	$val['times'];
			$tmp['MoneyModel']		=	$val['unit'];
			$tmp['BetNumber']		=	$val['multiple'];
			$tmp['Amount']			=	$val['money'];
			$tmp['KickBack']		=	0.0;
			$tmp['KickbackPercent']	=	0.0;//$val['KickbackPercent'];
			$tmp['KickbackAmount']	=	0.0;
			$tmp['OpenResult']		=	'';//$val['multiple'];
			$tmp['BettingBalance']	=	bcadd($val['prize'],-$val['money']);
			$tmp['PeriodStatus']	=	1;
			$tmp['OrderStatus']		=	0;
			$tmp['IsTrack']			=	false;
			$tmp['PeriodCloseTime']	=	date('Y-m-d H:i:s',$stoptime);
			
			$res[] = $tmp;
		}
		
		//	{"status":true,"info":"投注成功！","insertedOrder":"[{\"OrderId\":148528595,\"Ordernumber\":\"73008251039323605066\",\"PeriodId\":107620684,\"NumberOfPeriod\":\"20170825028\",\"GamePlayName\":\"五星复式\",\"CreateTime\":\"2017-08-25 10:39:32\",\"Content\":\"5|7|2|5|0\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2.0,\"KickBack\":0.0,\"KickbackPercent\":0.0,\"KickbackAmount\":0.0,\"OpenResult\":\"\",\"BettingBalance\":0.0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25T10:39:40\"}]"}
		//	{"status":true,"info":"投注成功！","data":"[{\"OrderId\":\"42\",\"Ordernumber\":\"42\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900960\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"1|9,2,6\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"},{\"OrderId\":\"43\",\"Ordernumber\":\"43\",\"PeriodId\":323,\"NumberOfPeriod\":\"170825-033\",\"GamePlayName\":\"\\u7ec4\\u900930\",\"CreateTime\":\"2017-08-25 11:26:42\",\"Content\":\"8,0|4\",\"Times\":1,\"MoneyModel\":1,\"BetNumber\":1,\"Amount\":2,\"KickBack\":0,\"KickbackPercent\":0,\"OpenResult\":\"\",\"BettingBalance\":0,\"PeriodStatus\":1,\"OrderStatus\":0,\"IsTrack\":false,\"PeriodCloseTime\":\"2017-08-25 11:29:40\"}]"}
		AjaxReturn(0,'下注成功！',$res);
	}
	else if($a == 'addcredit')	//	信用玩法
	{
		$iid = intval($_POST['periodID']);
		!$iid && AjaxReturn(1,'请选择彩票期号');
		
		$nums = $_POST['orderlist'];
		$nums = json_decode($nums,true);
		!is_array($nums) && AjaxReturn(1,'请选择要投注的号码');
		
		//	根据期号选择种类
		$tmp = $db->getrow('SELECT cid FROM '.$ltable.' WHERE id = '.$iid);
		!$tmp && AjaxReturn(1,'投注期号不存在');
		$cid = $tmp['cid'];
		
		$game = $lottery[$cid];
		!$game && AjaxReturn(1,'请选择彩票种类');
		
		//	期号
		$tmp = $db->getrow('SELECT id,period,stoptime FROM '.$ltable.' WHERE cid = '.$cid.' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime');
		!$tmp && AjaxReturn(1,'当前开盘期与投注期数不一致');
		($tmp['id'] != $iid) && AjaxReturn(1,'当前开盘期与投注期数不一致');
		$period = $tmp['period'];
		$stoptime = $tmp['stoptime'];
		
		//	是否最新赔率
		$rateforce = $_POST['force'] == 'true' ? 1 : 0;
		
		//	下注
		//include_once(LIB_PATH.'lottery/'.$game['ename'].'_buy.inc.php');
		include_once(LIB_PATH.'lottery/credit_buy.inc.php');
		!$orders && AjaxReturn(1,'请选择要投注的号码');
		($money < 0) && AjaxReturn(1,'请选择要投注的号码');
		
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['tid']		=	'order_add';
		$tmp['lkey']	=	'order_add_'.$_uid.'_'.$cid.date('_YmdHis');
		$tmp['money']	=	$money;
		$tmp['balance']	=	-$money;
		$tmp['notes']	=	'下注购买彩票：'.$game['title'];
		$res = MoneyLog($tmp);
		!$res && AjaxReturn(1,'账户余额不足，请先充值');
		
		//	订单
		$orderno = date('YmdHis').str_pad($cid,2,0,STR_PAD_LEFT).str_pad($_uid,6,0,STR_PAD_LEFT).rand(1000,9999);
		$tmp = [];
		$tmp['uid']		=	$_uid;
		$tmp['cid']		=	$cid;
		$tmp['iid']		=	$iid;
		$tmp['period']	=	$period;
		$tmp['orderno']	=	$orderno;
		$tmp['content']	=	json_encode($nums);
		$tmp['money']	=	$money;
		$tmp['dateline']=	TIMESTAMP;
		$orderid = $db->add('lottery_order',$tmp);
		//!$orderid && AjaxReturn(1,'程序大哥在打瞌睡，请重新提交');
		
		//	订单详单
		$res = [];	//	返回值
		foreach($orders as $key => $val)
		{
			$val['oid'] = $orderid;
			$oid = $db->add('lottery_order_list',$val);
			if(!$oid)
			{
				continue;
			}
			
			$tmp = [];
			$tmp['cTime']		=	date('H:i');
			$tmp['content']		=	$plays[$key]['name'].':'.$val['rednum'];
			$tmp['odds']		=	$val['odds'];
			$tmp['amount']		=	$val['money'];
			
			$res[] = $tmp;
		}
		
		//	{"status":true,"info":"下注成功","insertedOrder":"[{\"cTime\":\"15:52\",\"odds\":\"1.99\",\"amount\":\"1\",\"content\":\"第一球大小：小\"}]"}
		AjaxReturn(0,'下注成功！',$res);
	}
	
	AjaxReturn(0,'成功');
}

if($a == 'cancel')	//	撤单
{
	$cid = intval($_GET['gameid']);
	//!$cid && AjaxReturn(1,'彩票种类选择有误');
	
	$ids = NoBadStr($_GET['ids']);
	!$ids && AjaxReturn(1,'请选择要撤单的订单');
	
	$ids = explode(',',$ids);
	foreach($ids as $val)
	{
		$val = intval($val);
		if($val < 1)
		{
			continue;
		}
		
		$order = $db->getrow('SELECT l.tid,l.money,l.status,p.status AS pstatus FROM '.$oltable.' l LEFT JOIN '.$ltable.' p ON l.iid = p.id WHERE l.id = '.$val.' AND l.uid = '.$_uid);
		!$order && AjaxReturn(1,'撤单的订单不存在！');
		($order['pstatus'] == 2) && AjaxReturn(1,'该订单已经开奖中，不可撤销');
		($order['pstatus'] == 3) && AjaxReturn(1,'该订单已经开奖，不可撤销');
		($order['status'] != 0) && AjaxReturn(1,'该订单不可撤销');
		
		if($order['money'] > 0)
		{
			$tmp = [];
			$tmp['uid']		=	$_uid;
			$tmp['tid']		=	'order_cancel';
			$tmp['lkey']	=	'order_cancel_'.$_uid.'_'.$val;
			$tmp['money']	=	$order['money'];
			$tmp['balance']	=	$order['money'];
			$tmp['notes']	=	'撤销订单：'.$val;
			$res = MoneyLog($tmp);
			!$res && AjaxReturn(1,'撤销订单失败！');
		}
		
		//	撤销追号
		if($order['tid'] > 0)
		{
			$db->exec('UPDATE '.Table('lottery_order_track').' SET cancelmoney = cancelmoney + '.$order['money'].',cancelnum = cancelnum + 1 WHERE id = '.$order['tid']);
		}
		
		$db->exec('UPDATE '.$oltable.' SET status = 2 WHERE id = '.$val);
	}
	
	AjaxReturn(0,'撤单成功！');
}
else if($a == 'view')	//	详情
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('查看的投注不存在',-1);
	
	$data = $db->getrow('SELECT t.*,l.rednum AS result FROM '.$oltable.' t LEFT JOIN '.$ltable.' l ON t.iid = l.id WHERE t.uid = '.$_uid.' AND t.id = '.$id.' AND t.status != 3');
	!$data && ShowMsg('查看的投注不存在',-1);
	
	//	玩法
	$t2 = $db->getrow('SELECT name FROM '.$ptable.' WHERE playid = '.$data['pid'].' AND cid = '.$data['cid']);
	$data['name'] = $t2['name'];
	
	$data['gname'] = $lottery[$data['cid']]['title'];
	
	$tpl->display('member/order_view');
	exit;
}

$data  = [];
$where = ' FROM '.$oltable.' t LEFT JOIN '.$ltable.' l ON t.iid = l.id WHERE t.uid = '.$_uid.' AND t.status != 3';

$cid = intval($_GET['cid']);
$cid && $where .= ' AND t.cid = '.$cid;

//	玩法
$pid = intval($_GET['pid']);
if($cid && $pid)
{
	$tmp = $db->getrow('SELECT * FROM '.Table('lottery_play_json').' WHERE cid = '.$cid);
	$tmp = json_decode($tmp['content'],true);
	
	$cids = [];
	foreach($tmp as $val)
	{
		if($val['Id'] == $pid)
		{
			foreach($val['c'] as $val2)
			{
				foreach($val2['C'] as $val3)
				{
					if(is_array($val3['C']))
					{
						foreach($val3['C'] as $val4)
						{
							$cids[] = $val4['I'];
						}
					}
					else
					{
						$cids[] = $val3['I'];
					}
				}
			}
			break;
		}
	}
	
	$cids && $where .= ' AND t.pid IN ('.implode(',',$cids).')';
}

$dt = NoBadStr($_GET['dtrange']);
$dt = explode(' - ',$dt);
!$dt[0] && $dt[0] = date('Y-m-d',strtotime('-1 day'));
!$dt[1] && $dt[1] = date('Y-m-d');
$dtrange = implode(' - ',$dt);

$where .= ' AND t.dateline >= '.strtotime($dt[0]).' AND t.dateline < '.(strtotime($dt[1]) + 86400);

$page = max(1,intval($_GET['page']));
$size = max(20,intval($_GET['size']));

//	统计
$num = ['money'=>0,'investback'=>0,'prize'=>0,'money2'=>0,'investback2'=>0,'prize2'=>0];

//	状态	0正常单1已派奖2已撤单3已删除4下单失败99处理中
$status = intval($_GET['status']);
if($status == -1)	//	未开奖
{
	$where .= ' AND t.status = 0';
}
else if($status == 1)	//	未中奖
{
	$where .= ' AND t.status = 1 AND t.prize = 0';
}
else if($status == 2)	//	已中奖
{
	$where .= ' AND t.status = 1 AND t.prize > 0';
}

$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
if($tmp['p'])
{
	$orderby = ' ORDER BY t.id DESC ';
	$limit = ' LIMIT '.($page - 1) * $size.','.$size;
	
	$data = $db->getall('SELECT t.*,l.rednum AS result '.$where.$orderby.$limit);
	foreach($data as $key => $val)
	{
		//	玩法
		$t2 = $db->getrow('SELECT name FROM '.$ptable.' WHERE playid = '.$val['pid'].' AND cid = '.$val['cid']);
		$data[$key]['name'] = $t2['name'];
		
		$num['money'] += $val['money'];
		$num['investback'] += $val['investback'];
		$num['prize'] += $val['prize'];
	}
	
	$url = U($m.'/'.$c,['dtrange'=>$dtrange,'cid'=>$cid,'pid'=>$pid,'status'=>$status]);
	$multi = Multi($url,$page,$tmp['p'],$size);
	
	$tmp = $db->getrow('SELECT SUM(money) AS money,SUM(investback) AS investback,SUM(prize) AS prize '.$where);
	$num['money2'] = round($tmp['money'],2);
	$num['investback2'] = round($tmp['investback'],2);
	$num['prize2'] = round($tmp['prize'],2);
}

//	彩票种类
$cids = $lottery;

$head['title'] = '购彩记录';

$tpl->display('member/order');